package com.hanbang.gycatering.order;

import java.util.ArrayList;
import java.util.List;

import com.hanbang.gangyu.R;
import com.hanbang.gycatering.homepage.BaseFragmentActivity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class MyOrder extends BaseFragmentActivity {
	private List<Integer> flaglist;
	private Context context = this;
	private ViewPager viewpage;
	private TextView t1, t2, t3,t4;
	private TextView line1, line2, line3,line4;
	private int currIndex = 0;// 当前页卡编号

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.myorder);
		initTextView();
		initPageViewer();
	}

	/**
	 * 初始化标题
	 */
	private void initTextView() {
		((TextView)findViewById(R.id.top_bar_title)).setText("我的订单");
		t1 = (TextView) findViewById(R.id.text1);
		t2 = (TextView) findViewById(R.id.text2);
		t3 = (TextView) findViewById(R.id.text3);
		t4 = (TextView) findViewById(R.id.text4);
		
		line1 = (TextView) findViewById(R.id.line1);
		line2 = (TextView) findViewById(R.id.line2);
		line3 = (TextView) findViewById(R.id.line3);
		line4 = (TextView) findViewById(R.id.line4);

		t1.setOnClickListener(new MyOnClickListener(0));
		t2.setOnClickListener(new MyOnClickListener(1));
		t3.setOnClickListener(new MyOnClickListener(2));
		t4.setOnClickListener(new MyOnClickListener(3));
		setBackArrows(((ImageView) findViewById(R.id.top_bar_left_image)));
		 
	}
	/**
	 * 初始化PageViewer
	 */
	private void initPageViewer() {
		
		viewpage = (ViewPager) findViewById(R.id.viewpage);
		flaglist = new ArrayList<Integer>();
		flaglist.add(0);
		flaglist.add(1);
		flaglist.add(2);
		flaglist.add(3);
		viewpage.setAdapter(new PagerAdapter(getSupportFragmentManager(),flaglist));
		viewpage.setCurrentItem(currIndex);
		viewpage.setOnPageChangeListener(new MyOnPageChangeListener());
	}

	/**
	 * 页卡切换监听
	 * 
	 * @author Administrator
	 * 
	 */
	private class MyOnPageChangeListener implements OnPageChangeListener {
		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}

		@Override
		public void onPageSelected(int arg0) {
			currIndex = arg0;
			if(currIndex == 0){
				t1.setTextColor(getResources().getColor(R.color.orange));
				t2.setTextColor(getResources().getColor(R.color.black));
				t3.setTextColor(getResources().getColor(R.color.black));
				t4.setTextColor(getResources().getColor(R.color.black));
				line1.setVisibility(View.VISIBLE);
				line2.setVisibility(View.INVISIBLE);
				line3.setVisibility(View.INVISIBLE);
				line4.setVisibility(View.INVISIBLE);
			}else if(currIndex == 1){
				t1.setTextColor(getResources().getColor(R.color.black));
				t2.setTextColor(getResources().getColor(R.color.orange));
				t3.setTextColor(getResources().getColor(R.color.black));
				t4.setTextColor(getResources().getColor(R.color.black));
				line1.setVisibility(View.INVISIBLE);
				line2.setVisibility(View.VISIBLE);
				line3.setVisibility(View.INVISIBLE);
				line4.setVisibility(View.INVISIBLE);
			}else if(currIndex == 2){
				t1.setTextColor(getResources().getColor(R.color.black));
				t2.setTextColor(getResources().getColor(R.color.black));
				t3.setTextColor(getResources().getColor(R.color.orange));
				t4.setTextColor(getResources().getColor(R.color.black));
				line1.setVisibility(View.INVISIBLE);
				line2.setVisibility(View.INVISIBLE);
				line3.setVisibility(View.VISIBLE);
				line4.setVisibility(View.INVISIBLE);
			}else{
				t1.setTextColor(getResources().getColor(R.color.black));
				t2.setTextColor(getResources().getColor(R.color.black));
				t3.setTextColor(getResources().getColor(R.color.black));
				t4.setTextColor(getResources().getColor(R.color.orange));
				line1.setVisibility(View.INVISIBLE);
				line2.setVisibility(View.INVISIBLE);
				line3.setVisibility(View.INVISIBLE);
				line4.setVisibility(View.VISIBLE);
			}
		}
	}

	/**
	 * 头标点击监听
	 * 
	 * @author Administrator
	 * 
	 */
	private class MyOnClickListener implements OnClickListener {
		private int index;

		public MyOnClickListener(int index) {
			this.index = index;
		}

		@Override
		public void onClick(View v) {
			viewpage.setCurrentItem(index);
			if(index == 0){
				t1.setTextColor(getResources().getColor(R.color.orange));
				t2.setTextColor(getResources().getColor(R.color.black));
				t3.setTextColor(getResources().getColor(R.color.black));
				t4.setTextColor(getResources().getColor(R.color.black));
				line1.setVisibility(View.VISIBLE);
				line2.setVisibility(View.INVISIBLE);
				line3.setVisibility(View.INVISIBLE);
				line4.setVisibility(View.INVISIBLE);
			}else if(index == 1){
				t1.setTextColor(getResources().getColor(R.color.black));
				t2.setTextColor(getResources().getColor(R.color.orange));
				t3.setTextColor(getResources().getColor(R.color.black));
				t4.setTextColor(getResources().getColor(R.color.black));
				line1.setVisibility(View.INVISIBLE);
				line2.setVisibility(View.VISIBLE);
				line3.setVisibility(View.INVISIBLE);
				line4.setVisibility(View.INVISIBLE);
			}else if(index==2){
				t1.setTextColor(getResources().getColor(R.color.black));
				t2.setTextColor(getResources().getColor(R.color.black));
				t3.setTextColor(getResources().getColor(R.color.orange));
				t4.setTextColor(getResources().getColor(R.color.black));
				line1.setVisibility(View.INVISIBLE);
				line2.setVisibility(View.INVISIBLE);
				line3.setVisibility(View.VISIBLE);
				line4.setVisibility(View.INVISIBLE);
			}
			else{
				t1.setTextColor(getResources().getColor(R.color.black));
				t2.setTextColor(getResources().getColor(R.color.black));
				t3.setTextColor(getResources().getColor(R.color.black));
				t4.setTextColor(getResources().getColor(R.color.orange));
				line1.setVisibility(View.INVISIBLE);
				line2.setVisibility(View.INVISIBLE);
				line3.setVisibility(View.INVISIBLE);
				line4.setVisibility(View.VISIBLE);
			}
		}
	}
	/**
	 * Pager适配器
	 * 
	 * @author Administrator
	 * 
	 */
	class PagerAdapter extends FragmentPagerAdapter {
		 private List<Integer> flaglist;
		 public PagerAdapter(FragmentManager fm) {
		        super(fm);
		    }
		public PagerAdapter(FragmentManager fm,List<Integer> list) {
			super(fm);
			this.flaglist = list;
		}

		@Override
		public Fragment getItem(int index) {
			return  new OrderFragment(flaglist.get(index)) ;
		}

		 

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			return super.instantiateItem(container, position);
		}

		@Override
		public int getItemPosition(Object object) {
			return PagerAdapter.POSITION_NONE;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return flaglist.size();
		}
	}
}
